package com.yvon.maple.service.system.service.impl;

import com.yvon.boot.mybatis.BaseServiceImpl;
import com.yvon.maple.service.system.mapper.RoleMapper;
import com.yvon.maple.service.system.entity.Role;
import com.yvon.maple.service.system.service.RolePermissionService;
import com.yvon.maple.service.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.yvon.maple.constants.SystemConstant.Common.LIMIT_ONE;

/**
 * @author : Yvon
 * @version : 1.0
 * @since : 2021/3/15
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role, RoleMapper> implements RoleService {

    private static final String COLUMN_CODE = "code";

    @Autowired
    private RolePermissionService rolePermissionService;

    @Override
    public boolean save(Role role, List<Long> permissionIds) {
        checkUniqueness(COLUMN_CODE, role.getCode(), null, "该角色标识已存在");
        return save(role) && rolePermissionService.save(role.getId(), permissionIds);
    }

    @Override
    public boolean update(Role role, List<Long> permissionIds) {
        checkUniqueness(COLUMN_CODE, role.getCode(), role.getId(), "该角色标识已存在");
        return updateById(role) && rolePermissionService.update(role.getId(), permissionIds);
    }

    @Override
    public Role getByCode(String code) {
        return lambdaQuery()
                .eq(Role::getCode, code)
                .last(LIMIT_ONE)
                .one();
    }
}
